# coding: utf8
from . import config
import pickle
import argparse
import logging
import pandas as pd
import numpy as np


def get_num(value):
    try:
        if value.find('.') == -1:
            value = int(value)
        else:
            value = float(value)
    except:
        pass
    return value

def could_override_config():
    '''
    覆盖正常参数 用 key=value。覆盖某些关联属性会自动修正，如task和dataset。task和dataset最好只传一个！
    覆盖模型参数 用 --key value
    '''
    parser = argparse.ArgumentParser()
    args, unknown = parser.parse_known_args()

    n = len(unknown)
    i = 0
    while i < n:
        if unknown[i].find('=') != -1:
            key = unknown[i].split('=')[0]
            value = get_num(unknown[i].split('=')[1])
            setattr(config, key, value)
            i += 1
            if key == 'dataset':
                '''特殊处理'''
                if value == 'val':
                    config.task = 'val'
                elif value == 'test':
                    config.task = 'test'
                else:
                    raise Exception('dataset error')
            if key == 'task':
                '''特殊处理'''
                if value == 'val' or value == 'valall':
                    config.dataset = 'val'
                elif value == 'test':
                    config.dataset = 'test'
                else:
                    raise Exception("task error")
        elif unknown[i].startswith('--'):
            key = unknown[i][2:len(unknown[i])]
            value = get_num(unknown[i + 1])
            config.model_para[key] = value
            print('warning: overide "config.model_para["%s"]" Now: %s=%s' % (key, key, str(value)))
            i += 2
        else:
            raise Exception('Error!')